package com.example.netimage;

import com.example.netimage.util.LogUtil;
import com.example.netimage.util.UtilNetImage;
import com.zzrv5.mylibrary.ZZRCallBack;
import com.zzrv5.mylibrary.ZZRHttp;
import com.zzrv5.mylibrary.ZZRResponse;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Image;
import ohos.app.Context;
import ohos.agp.components.element.Element;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
public class NetImage extends Image {
    private Element errorElement=null;//加载失败图片
    private PixelMap mPixelMap=null;//暂时没用
    private AttrSet attrSet=null;
    public NetImage(Context context,String url) {
        super(context);
        if(UtilNetImage.isHttpUrl(url)){
            LogUtil.error("url",url);
            ZZRHttp.get(url, new ZZRCallBack.CallBackString() {
                @Override
                public void onFailure(int code, String errorMessage) {
                    //http访问出错了，此部分内容在主线程中工作;
                    //可以更新UI等操作,请不要执行阻塞操作。

                    context.getUITaskDispatcher().asyncDispatch(new Runnable() {
                        @Override
                        public void run() {
                            //展示到组件上
                            LogUtil.error("networkErr",errorMessage);
                            System.out.println("errorMessage"+errorMessage);
                            if(errorElement!=null){
                                setImageElement(errorElement);
                            }else {
                                setPixelMap(ResourceTable.Media_error);
                            }
                        }
                    });
                }
                @Override
                public String onParseResponse(ZZRResponse response) {
                    //创建图片源
                    ImageSource imageSource = ImageSource.create(response.inputStream, null);
                    //根据图片源创建位图
                    PixelMap pixelMap = imageSource.createPixelmap(null);
                    //需要异步渲染UI
                    context.getUITaskDispatcher().asyncDispatch(new Runnable() {
                        @Override
                        public void run() {
                            //展示到组件上
                            setPixelMap(pixelMap);
                            pixelMap.release();
                        }
                    });
                    return super.onParseResponse(response);
                }
                @Override
                public void onResponse(String response) {

                }
            });
        }else {
            if(errorElement!=null){
                setImageElement(errorElement);
            }else {
                setPixelMap(ResourceTable.Media_error);
            }
            LogUtil.error("dataERROR","非法参数src ");
        }
    }
    /**
     *
     * @param context
     * @param attrSet  定义如何存储、读取和使用与组件控件相关的属性，以及需要使用属性集的其他类。
     */
    public NetImage(Context context, AttrSet attrSet) {
        super(context, attrSet);

        if (attrSet != null){
        this.attrSet = attrSet;
            attrSet.getAttr("src_error").ifPresent(attr ->errorElement=attr.getElement());
            attrSet.getAttr("src").ifPresent(attr ->{

                String stringValue = attr.getStringValue();
                if(UtilNetImage.isHttpUrl(stringValue)){
                    LogUtil.error("url",stringValue);
                    ZZRHttp.get(stringValue, new ZZRCallBack.CallBackString() {
                        @Override
                        public void onFailure(int code, String errorMessage) {
                            //http访问出错了，此部分内容在主线程中工作;
                            //可以更新UI等操作,请不要执行阻塞操作。

                            context.getUITaskDispatcher().asyncDispatch(new Runnable() {
                                @Override
                                public void run() {
                                    //展示到组件上
                                    LogUtil.error("networkErr",errorMessage);
                                    System.out.println("errorMessage"+errorMessage);
                                    if(errorElement!=null){
                                        setImageElement(errorElement);
                                    }else {
                                        setPixelMap(ResourceTable.Media_error);
                                    }
                                }
                            });
                        }
                        @Override
                        public String onParseResponse(ZZRResponse response) {
                            //创建图片源
                            ImageSource imageSource = ImageSource.create(response.inputStream, null);
                            //根据图片源创建位图
                            PixelMap pixelMap = imageSource.createPixelmap(null);
                            //需要异步渲染UI
                            context.getUITaskDispatcher().asyncDispatch(new Runnable() {
                                @Override
                                public void run() {
                                    //展示到组件上
                                    setPixelMap(pixelMap);
                                    pixelMap.release();
                                }
                            });
                            return super.onParseResponse(response);
                        }
                        @Override
                        public void onResponse(String response) {

                        }
                    });
                }else if(UtilNetImage.isLocalData(stringValue)){
                    setImageElement(attr.getElement());
                }else {
                    if(errorElement!=null){
                        setImageElement(errorElement);
                    }else {
                        setPixelMap(ResourceTable.Media_error);
                    }
                    LogUtil.error("dataERROR","非法参数src ");
                }

            });
        }else {
            System.out.println("dataERROR 没初始化 attrset");
            if(errorElement!=null){
                setImageElement(errorElement);
            }else {
                setPixelMap(ResourceTable.Media_error);
            }
        }
    }

    public NetImage(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
    }




}
